package com.linkvin.ec.hcs.service.mdm.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.linkvin.ec.hcs.dao.mapper.mdm.DepartmentMapper;
import com.linkvin.ec.hcs.dao.model.mdm.Department;
import com.linkvin.ec.hcs.dao.model.mdm.bo.DepartmentBo;
import com.linkvin.ec.hcs.service.mdm.DepartmentService;
import com.linkvin.ec.hcs.service.util.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public Page<DepartmentBo> queryPageDepartment(Page<DepartmentBo> page) {
        List<DepartmentBo> listDepartBo = departmentMapper.queryPageDepartment(page,buildCondition(page.getCondition()));
        page.setRecords(listDepartBo);
        return page;
    }

    @Override
    public DepartmentBo addDepartment(Department department) {

        DepartmentBo departmentBO = new DepartmentBo();

        //非空校验
        if (Strings.isNullOrEmpty(department.getCode().trim())) throw new BaseException("部门编码不能为空");
        if (Strings.isNullOrEmpty(department.getName().trim())) throw new BaseException("部门名称不能为空");
        if (Objects.equals(department.getOrgId(),null)) throw new BaseException("所属组织不能为空");

        //判断是否重复
        EntityWrapper<Department> ew = new EntityWrapper<>();
        ew.where("code={0}",department.getCode().trim()).or("name={0}",department.getName().trim());
        int count = departmentMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("部门编码或者编码重复");
        }

        count = departmentMapper.insert(department);

        if(count > 0){
            departmentBO = getDepartemntBo(department.getCode());
        }

        return  departmentBO;


    }

    @Override
    public DepartmentBo updateDepartment(Department department) {
        DepartmentBo departmentBO = new DepartmentBo();

        //非空校验
        if (Strings.isNullOrEmpty(department.getCode().trim())) throw new BaseException("部门编码不能为空");
        if (Strings.isNullOrEmpty(department.getName().trim())) throw new BaseException("部门名称不能为空");
        if (Objects.equals(department.getOrgId(),null)) throw new BaseException("所属组织不能为空");

        //判断是否重复
        EntityWrapper<Department> ew = new EntityWrapper<>();
        ew.where("id != {0}",department.getId()).andNew("code={0}",department.getCode().trim()).or("name={0}",department.getName().trim());
        int count = departmentMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("部门编码或者编码重复");
        }

        count = departmentMapper.updateById(department);

        if(count > 0){
            departmentBO = getDepartemntBo(department.getCode());
        }

        return  departmentBO;
    }

    @Override
    public int deleteDepartments(List<Integer> departmentIds) {
        //TODO 需要判断是否已经使用
        return departmentMapper.deleteBatchIds(departmentIds);
    }

    private DepartmentBo getDepartemntBo(String code){
        DepartmentBo departmentBO = new DepartmentBo();
        List<DepartmentBo> listDepartBo= departmentMapper.selectDepartment(code);
        if(!CollectionUtils.isEmpty(listDepartBo)){
            departmentBO = listDepartBo.get(0);
        }
        return departmentBO;
    }

    private EntityWrapper<DepartmentBo> buildCondition(Map<String,Object> condition){
        EntityWrapper<DepartmentBo> ew = new EntityWrapper<>();
        if(!Objects.equals(condition,null)) {
            for (Map.Entry<String, Object> entry : condition.entrySet()) {
                if (entry.getKey().equals("code")) {
                   ew.eq("code", entry.getValue().toString());
                }
                if (entry.getKey().equals("name")) {
                    ew.like("name", entry.getValue().toString());
                }
                if (entry.getKey().equals("orgId")) {
                    ew.eq("org_id", entry.getValue().toString());
                }
                if (entry.getKey().equals("orgCode")) {
                    ew.eq("org_code", entry.getValue().toString());
                }
                if (entry.getKey().equals("orgName")) {
                    ew.eq("org_name", entry.getValue().toString());
                }
            }
        }
        return ew;
    }


}
